﻿using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web;
using System.Security.Policy;
using System.Collections.Generic;
using Epam.PhotoGallery.Site.Helpers;
using Epam.PhotoGallery.Interfaces.DataTransferObjects;
using Epam.PhotoGallery.Site.Models;
using Epam.PhotoGallery.Bussines.Configuration;
using System.Configuration;

namespace Epam.PhotoGallery.Site.Models
{
    public static class HtmlHelperExtentions
    {
        private const char DomainSeparator = '\\';
        private const char UnderLineSeparator = '_';
        private const char WhiteSpace = ' ';

        private const string TAG_NAME_A = "a";
        private const string CSS_CLASS_SELECTED = "selected";
        private const string HREF_ATTRIBUTE_NAME = "href";



        public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
            var sb = new StringBuilder();

            if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
            {

                sb.Append("<li class=\"active\">");
            }
            else
            {
                sb.Append("<li class=\"noactive\">");
            }

            sb.Append(helper.ActionLink(linkText, actionName, controllerName));
            sb.Append("</li>");

            return sb.ToString();
        }

        public static string FormatUserName(string userName)
        {
            var result = string.Empty;

            if (!string.IsNullOrEmpty(userName))
            {
                var domainSeparatorIndex = userName.IndexOf(DomainSeparator);
                if (domainSeparatorIndex > 0)
                {
                    result = userName.Substring(domainSeparatorIndex + 1)
                        .Replace(UnderLineSeparator, WhiteSpace)
                        .ToUpperInvariant();
                }
            }

            return result.ToString();
        }

        public static string User(this HtmlHelper helper)
        {
            var sb = new StringBuilder();
            sb.Append("<p style=\"color: White; float: right;\"> WELCOME ");
            sb.Append(FormatUserName(HttpContext.Current.User.Identity.Name));
            sb.Append("<p>");

            return sb.ToString();
        }

        public static string GeneratePageLinks(this HtmlHelper helper, int currentPage, int pagesTotal, Func<int, string> pageUrl)
        {
            const int OFFSET_PAGE = 5;
            const int NUMBER_OF_PAGES_DISPLAYS = 10;

            StringBuilder resultUrl = new StringBuilder();
            int startPage = 1;

            if (currentPage > OFFSET_PAGE)
            {
                startPage = currentPage - OFFSET_PAGE;
            }

            int count = 0;
            
            while (startPage<=pagesTotal && count < NUMBER_OF_PAGES_DISPLAYS )
            {

                TagBuilder tagA = new TagBuilder(TAG_NAME_A);
                tagA.MergeAttribute(HREF_ATTRIBUTE_NAME, pageUrl(startPage));
                tagA.InnerHtml = startPage.ToString();

                if (startPage == currentPage)
                {
                    tagA.AddCssClass(CSS_CLASS_SELECTED);
                }

                resultUrl.AppendLine(tagA.ToString());

                count++;
                startPage++;
            }

            return resultUrl.ToString();
        }

 
public static HeaderItemsCollection Scripts(this HtmlHelper htmlHelper)
		{
			var context = htmlHelper.ViewContext.HttpContext;
			if (context == null)
			{
				return null;
			}

			var instanceKey = "ScriptsHeaderItemsCollection";
			var blockEditor = (HeaderItemsCollection)context.Items[instanceKey];

			if (blockEditor == null)
			{
				blockEditor = new ScriptHeaderItemsCollection();
				context.Items.Add(instanceKey, blockEditor);
			}

			return blockEditor;
		}

		public static HeaderItemsCollection Styles(this HtmlHelper htmlHelper)
		{
			var context = htmlHelper.ViewContext.HttpContext;
			if (context == null)
			{
				return null;
			}

			var instanceKey = "StylesHeaderItemsCollection";
			var blockEditor = (HeaderItemsCollection)context.Items[instanceKey];

			if (blockEditor == null)
			{
				blockEditor = new StyleHeaderItemsCollection();
				context.Items.Add(instanceKey, blockEditor);
			}

			return blockEditor;
		}
		public static int[] GetGellaryControlImageSize(this HtmlHelper htmlHelper)
		{
			GalleryControlBlockSizeSection GalleryBlok = (GalleryControlBlockSizeSection)ConfigurationManager.GetSection(GalleryControlBlockSizeSection.GalleryControlBlockSizeSectionName);
			int CHeight = GalleryBlok.Height;
			int CWidth = GalleryBlok.Width;
			int CECount = GalleryBlok.Count;

			int h = (int)Math.Floor((double)(CHeight / 3));
			int w = (int)Math.Floor((double)(h * CWidth / CHeight));
			int hh = CHeight - h;
			int ww = w * 2;
			int www = CWidth - ww;
			int hhh = CHeight;
			int[] result = new[] { CECount, CWidth, CHeight, w + 80, h + 120, w + 80, h + 120, ww + 80, hh + 120, www + 80, hhh + 120 };

			return result;
		}

    }
}